package com.usian.mapper;
import com.usian.feedback.Feedback;
import com.usian.game.GameLogs;
import com.usian.role.Role;
import com.usian.room.Room;
import com.usian.user.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 米西米西
 */
@Mapper
public interface SystemMapper {
    @Select("<script>" +
            "SELECT * FROM `room`" +
            "<where>" +
            "    1=1" +
            "    <if test='roomNumber != null'> " +
            "        AND room_number = #{roomNumber}" +
            "    </if>" +
            "    <if test='roomName != null and roomName.trim() != \"\"'> " +
            "        AND room_name LIKE CONCAT('%', #{roomName}, '%') " +
            "    </if>" +
            "</where>" +
            "</script>")
    List<Room> findRoom(Room room);

    @Select("select name from user where id = #{roomUser}")
    String findUserName(Integer roomUser);

    @Select("<script>" +
            "SELECT * FROM user" +
            "<where>" +
            "    1=1" +
            "    <if test='name != null and name.trim() != \"\"'> " +
            "        AND name LIKE CONCAT('%', #{name}, '%') " +
            "    </if>" +
            "</where>" +
            "</script>")
    List<User> userFindAll(User user);

    @Select("select user.* from user join room_user ru on user.id = ru.user_id where ru.room_id = #{id}")
    List<User> userFindUserId(Integer id);

    @Select("<script>" +
            "SELECT * FROM role" +
            "<where>" +
            "    1=1" +
            "    <if test='roleName != null and roleName.trim() != \"\"'> " +
            "        AND role_name LIKE CONCAT('%', #{roleName}, '%') " +
            "    </if>" +
            "</where>" +
            "</script>")
    List<Role> findRolePage(Role role);

    @Delete("delete from room_user where room_id = #{roomid} and user_id = #{userId}")
    void delRoomUsers(Integer roomid, Integer userId);

    @Select("select room_user from room where id = #{roomid}")
    Integer getRoomUser(Integer roomid);

    @Select("select * from game_logs where room_id = #{roomid}")
    List<GameLogs> getGameUserLogs(Integer roomid);

    @Update("UPDATE user " +
            "SET " +
            "    name = #{name}," +
            "    account = #{account}, " +
            "    password = #{password}, " +
            "    sex = #{sex},  " +
            "    img = #{img},  " +
            "    age = #{age},  " +
            "    charisma = #{charisma},  " +
            "    accrued_games = #{accruedGames},  " +
            "    introduction = #{introduction}  " +
            "WHERE id = #{id}; ")
    Integer updateUser(User user);

    @Select("<script>" +
            "SELECT * FROM feedback" +
            "<where>" +
            "    1=1" +
            "    <if test='userId != null'> " +
            "        AND userId = #{userId} " +
            "    </if>" +
            "</where>" +
            "</script>")
    List<Feedback> getFeedback(Feedback feedback);
}
